package com.jianqiao.delivery.delivery.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jianqiao.delivery.delivery.domain.DeliveryEntity;
import com.jianqiao.delivery.delivery.domain.dto.CountDeliveryDto;
import com.jianqiao.delivery.delivery.domain.TGlPackageDetailsEntity;
import com.jianqiao.delivery.delivery.domain.vo.DeliveryStatusVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * author: cyx
 * create: 2024/5/24 17:48
 **/
@Repository
public interface DeliveryMapper extends BaseMapper<DeliveryEntity> {

    @Select("SELECT\n" +
            "\td.id, \n" +
            "\td.tracking_number, \n" +
            "\td.weight, \n" +
            "\td.number_of_pieces, \n" +
            "\td.shipping_address, \n" +
            "\td.transportation_method, \n" +
            "\td.shipping_channel, \n" +
            "\td.payment_method, \n" +
            "\td.packaging_services, \n" +
            "\td.recipient_name, \n" +
            "\td.recipient_phone_one, \n" +
            "\td.recipient_phone_tow, \n" +
            "\td.receiver_address, \n" +
            "\td.choose_address, \n" +
            "\td.nation, \n" +
            "\td.province, \n" +
            "\td.city, \n" +
            "\tdd1.label AS payLabel, \n" +
            "\tdd2.label AS transportLabel\n" +
            "FROM\n" +
            "\tdict_detail AS dd1\n" +
            "\tINNER JOIN\n" +
            "\tdelivery AS d\n" +
            "\tON \n" +
            "\t\td.payment_method = dd1.`value`\n" +
            "\tINNER JOIN\n" +
            "\tdict_detail AS dd2\n" +
            "\tON \n" +
            "\t\td.transportation_method = dd2.`value`\n" +
            "WHERE\n" +
            "\tdd1.dict_id = 18 AND\n" +
            "\tdd2.dict_id = 19")
    List<DeliveryEntity> selectDeliveryList();

    @Select("SELECT\n" +
            "\tdelivery.`status` AS statusName, \n" +
            "\tCOUNT(delivery.id) AS statusCode\n" +
            "FROM\n" +
            "\tdelivery\n" +
            "\tINNER JOIN\n" +
            "\tt_gl_package_details\n" +
            "\tON \n" +
            "\t\tdelivery.order_number = t_gl_package_details.order_number\n" +
            "WHERE\n" +
            "\tdelivery.del_flag = 1 AND\n" +
            "\tt_gl_package_details.del_flag = 1\n" +
            "GROUP BY\n" +
            "\tdelivery.`status`")
    List<CountDeliveryDto> countDelivery();

    /**
      *methood: 插入快递表
      *auth: cyx
      *time: 2024/6/9 18:37
    **/
    int insertDelivery(List<DeliveryEntity> deliveryList);

    /**
      *methood: 插入包裹表
      *auth: cyx
      *time: 2024/6/9 18:37
    **/
    int insertPackage(List<TGlPackageDetailsEntity> packageList);

    /**
      *methood: 查询快递单列表
      *auth: cyx
      *time: 2024/6/9 19:29
    **/
    List<DeliveryEntity>selectDeliveryList(@Param("deliveryStatus") String deliveryStatus ,
                                           @Param("createBy") String createBy);

    /**
      *methood: 查询详情
      *auth: cyx
      *time: 2024/6/9 20:35
    **/
    DeliveryEntity selectDeliveryDetail(@Param("deliveryId") String deliveryId);

    int insertCity(List<Map<String ,String>> cityList);

    /**
      *methood: 根据单号查快递
      *auth: cyx
      *time: 2024/11/30 14:56
    **/
    List<DeliveryEntity> listByOrderNumber(@Param("orderNumber") String orderNumber);

    /**
      *methood: 根据包裹id查快递订单及包裹信息
      *auth: cyx
      *time: 2024/12/6 14:03
    **/
    DeliveryEntity detailByPackageId(@Param("packageId") String packageId);

    DeliveryEntity getDeliverDetail(String deliveryId);

    /**
      *methood: 修改包裹状态
      *auth: cyx
      *time: 2024/12/7 9:16
    **/
    Integer changePackageStatus(@Param("packageId") String packageId ,
                                @Param("statusValue") String statusValue);

    /**
      *methood: 查询订单及包裹状态
      *auth: cyx
      *time: 2024/12/7 9:30
    **/
    DeliveryStatusVo selectDeliveryStatus(@Param("packageId") String packageId);
}
